package ATM.data;

import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import ATM.data.AtmData;
import ATM.gui.Screen;

public class CustomData extends AtmData {
	private PreparedStatement selectCustomersAccounts = null;
	private PreparedStatement selectSpecificAccount= null;

	public CustomData(Screen screen) {
		super(screen);

		try {
			selectCustomersAccounts = connection
					.prepareStatement("SELECT * FROM Accounts WHERE CustomerID = ?");
			selectSpecificAccount = connection
					.prepareStatement("SELECT * FROM Accounts WHERE AccountID = ?");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public ArrayList<Accounts> getCustomersAccounts(int customerID) {
		ArrayList<Accounts> results = null;

		try {
			selectCustomersAccounts.setInt(1, customerID);
			resultSet = selectCustomersAccounts.executeQuery();
			results = new ArrayList<Accounts>();

			while (resultSet.next()) {
				results.add(new Accounts(resultSet.getInt("AccountID"),
						resultSet.getString("AccountType"), resultSet
								.getDouble("AvailableBalance"), resultSet
								.getDouble("TotalBalance"), resultSet
								.getDouble("Overdraft"), resultSet
								.getDouble("Interest")));
			}
		} catch (SQLException sqlException) {
			sqlException.printStackTrace();
			close();
			closeR();

		}
		return results;

	}

	public int getAccount(String customerid) {
		int aID = 0;

		try {
			resultSet = statement
					.executeQuery("SELECT AccountID FROM Accounts WHERE CustomerID = "
							+ customerid);
			resultSet.next();
			aID = resultSet.getInt("AccountID");
		} catch (SQLException sqlException) {
			sqlException.printStackTrace();
			close();
			closeR();

		}
		return aID;
	}// end method

	public void WithdrawCash(double amount, int accountID) {
		try {
			selectSpecificAccount.setInt(1, accountID);
			resultSet = selectSpecificAccount.executeQuery();
			resultSet.next();
			double avb = resultSet.getDouble("AvailableBalance");
			double ovd = resultSet.getDouble("OverDraft");
			double ttb = resultSet.getDouble("TotalBalance");
			if (amount <= avb + ovd) {
				avb -= amount;
				ttb -= amount;
				updateAvBal.setDouble(1, avb);
				updateAvBal.setInt(2, accountID);
				updateAvBal.executeUpdate();

				updateToBal.setDouble(1, ttb);
				updateToBal.setInt(2, accountID);
				updateToBal.executeUpdate();
			}
		} catch (SQLException sqlException) {
			sqlException.printStackTrace();
			close();
			closeR();

		}
	}

	public void DepositAmount(double amount, int accountID) {
		try {
			selectSpecificAccount.setInt(1, accountID);
			resultSet = selectSpecificAccount.executeQuery();
			resultSet.next();
			double ttb = resultSet.getDouble("TotalBalance");
			ttb += amount;

			updateToBal.setDouble(1, ttb);
			updateToBal.setInt(2, accountID);
			updateToBal.executeUpdate();

		} catch (SQLException sqlException) {
			sqlException.printStackTrace();
			close();
			closeR();

		}

	}

}
